class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode *> q;
        q.push(root);
        int result = 0;
        while (!q.empty()) {
            int size = q.size();
            result++;
            for (int i = 0; i < size; ++i) {
                TreeNode *node = q.front();
                q.pop();
                if (!node->left && !node->right) return result;
                else {
                    if (node->left) q.push(node->left);
                    if (node->right) q.push(node->right);
                }
            }
        }
        return result;
    }

    int minDepth_1(TreeNode* root) {
        if (!root) return 0;
        else if (!root->left && !root->right) return 1;
        else {
            int result = INT_MAX;
            if (root->left) result = min (result, minDepth(root->left));
            if (root->right) result = min (result, minDepth(root->right));
            return result + 1;
        }
    }
};
